CREATE TABLE [dbo].[tb_FormulasDeConcepto]
(
[idFormula] [int] NOT NULL IDENTITY(1, 1),
[Concepto] [int] NOT NULL,
[Orden] [int] NOT NULL,
[Condicion] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Calculo] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Desde] [datetime] NOT NULL,
[Hasta] [datetime] NULL,
[Vigente] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updOrden] ON [dbo].[tb_FormulasDeConcepto] 
FOR UPDATE
AS
declare @ordenNuevo int
declare @ordenViejo int
declare @Concepto int

Select @ordenNuevo = orden from inserted
Select @ordenViejo = orden from deleted
Select @Concepto = concepto from inserted

if exists (Select * from tb_FormulasDeConcepto where concepto = @concepto and orden = @OrdenNuevo)
	update tb_FormulasDeConcepto set orden = @OrdenViejo where concepto = @concepto and orden = @ordennuevo
GO
ALTER TABLE [dbo].[tb_FormulasDeConcepto] ADD CONSTRAINT [PK_tb_FormulasDeConcepto] PRIMARY KEY NONCLUSTERED  ([idFormula]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_FormulasDeConcepto] WITH NOCHECK ADD CONSTRAINT [FK_tb_FormulasDeConcepto_tb_ConceptosDeLiquidacion] FOREIGN KEY ([Concepto]) REFERENCES [dbo].[tb_ConceptosDeLiquidacion] ([idConceptoLiquidacion])
GO
